import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class SolutionInterview10_2 {
    public List<List<String>> groupAnagrams(String[] strs) {
        Map<String,List<String>> map=new HashMap<>();
        for (String str : strs) {
            int[] r=new int[26];
            for (char c : str.toCharArray()) {
                r[c-'a']++;
            }
            StringBuilder stringBuilder=new StringBuilder();
            for (int i=0;i<26;i++){
                if (r[i]>0){
                    stringBuilder.append((char)(i+'a'));
                    stringBuilder.append(r[i]);
                }
            }
            String string=stringBuilder.toString();
            List<String> l=map.getOrDefault(string,new ArrayList<>());
            l.add(str);
            map.put(string,l);
        }
        return new ArrayList<>(map.values());
    }

    public static void main(String[] args) {
        System.out.println(new SolutionInterview10_2().groupAnagrams(new String[]{"eat", "tea", "tan", "ate", "nat", "bat"}));
    }
}
